﻿using System;
using System.Windows.Controls;
using System.Collections;
using System.Windows.Documents;
using System.Windows;

namespace CodeCounsel.SharePoint.RibbonDesigner.View.AttachedBehaviors
{
    class ItemsControlDropManager
        : DropManager
    {
        ItemsControl _targetItemsControl;
        bool _hasVerticalOrientation;
        int _insertionIndex;
        bool _isInFirstHalf;
        InsertionAdorner _insertionAdorner;

        public ItemsControlDropManager(ItemsControl targetItemsControl, IDropProcessor dropProcessor)
            : base(dropProcessor)
        {
            _targetItemsControl = targetItemsControl;
            TargetItemContainer = targetItemsControl;
        }

        public override void CreateInsertionAdorner()
        {
            if (TargetItemContainer != null)
            {
                var adornerLayer = AdornerLayer.GetAdornerLayer(TargetItemContainer);
                _insertionAdorner = new InsertionAdorner(_hasVerticalOrientation, _isInFirstHalf, TargetItemContainer, adornerLayer);
            }
        }

        public override void UpdateInsertionAdornerPosition()
        {
            _insertionAdorner.IsInFirstHalf = _isInFirstHalf;
            _insertionAdorner.InvalidateVisual();
        }

        public override void RemoveInsertionAdorner()
        {
            if (_insertionAdorner != null)
            {
                _insertionAdorner.Detach();
                _insertionAdorner = null;
            }
        }

        public override DragDropEffects DecideDropTarget(DependencyObject originalSource, Func<IInputElement, Point> getDropPoint)
        {
            int targetItemsControlCount = _targetItemsControl.Items.Count;
            if (targetItemsControlCount > 0)
            {
                _hasVerticalOrientation = Utilities.HasVerticalOrientation(_targetItemsControl.ItemContainerGenerator.ContainerFromIndex(0) as FrameworkElement);
                TargetItemContainer = _targetItemsControl.ContainerFromElement(originalSource) as FrameworkElement;
                if (TargetItemContainer != null)
                {
                    Point positionRelativeToItemContainer = getDropPoint(TargetItemContainer);
                    _isInFirstHalf = Utilities.IsInFirstHalf(TargetItemContainer, positionRelativeToItemContainer, _hasVerticalOrientation);
                    _insertionIndex = _targetItemsControl.ItemContainerGenerator.IndexFromContainer(TargetItemContainer);

                    if (!_isInFirstHalf)
                    {
                        _insertionIndex++;
                    }
                }
                else
                {
                    TargetItemContainer = _targetItemsControl.ItemContainerGenerator.ContainerFromIndex(targetItemsControlCount - 1) as FrameworkElement;
                    _isInFirstHalf = false;
                    _insertionIndex = targetItemsControlCount;
                }
            }
            else
            {
                TargetItemContainer = null;
                _insertionIndex = 0;
            }
            return DragDropEffects.All;
        }

        public override void InsertDraggedItem(object draggedItem, DragManager sourceHelper, bool move, int originalIndex)
        {
            ItemsControlDragManager itemsSourceHelper = sourceHelper as ItemsControlDragManager;
            if (itemsSourceHelper != null)
            {
                if (sourceHelper.DragProcessor == DropProcessor && move && originalIndex < _insertionIndex)
                {
                    _insertionIndex--;
                }
            }
            DropProcessor.PerformDrop(draggedItem, _insertionIndex);
        }
    }
}
